<?php

namespace app\savant\controller;

use think\Request;
use think\facade\Log;
use app\common\enums\ResultEnum;
use app\common\exception\ZeduException;
use app\common\model\UserTags;
use app\common\model\Video as VideoModel;
use app\common\validate\Video as VideoValidate;
use app\common\utils\ResultVOUtil;
use app\common\utils\OssFlies;

class Video extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($search='',$length=10,$page=1)
    {
        $where[] = ['author','=',session('savant.pid')];
        if ( !empty($search) ) {
            $where[] = ['title','like','%'.$search.'%'];
        }
        try {
            $model = VideoModel::where($where)
            ->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            
        } catch (\Exception $e) {
            Log::record('[视频管理]列表:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
        return ResultVOUtil::success($model);
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $validate = new VideoValidate();
        
        if (!$validate->scene('base')->check($data)) {
            throw new ZeduException(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>$validate->getError()]);
        }
        if(count($data['cateValue'])>1){
            $data['cid'] = $data['cateValue'][1];
        }else{
            $data['cid'] = $data['cateValue'][0];
        }
        if(!empty($data['userCateValue'])){            
            if(count($data['userCateValue'])>1){
                $data['ucid'] = $data['userCateValue'][1];
            }else{
                $data['ucid'] = $data['userCateValue'][0];
            }
        }
        if(count($data['tagSelected'])>0){
            //存储到tags表数次数
            $data['tags'] = implode(',', $data['tagSelected']);
            try {
                
                UserTags::whereIn('id',$data['tagSelected'])->setInc('count');
            } catch (\Exception $e) {
                Log::record('[视频管理]新增:'.$e->getMessage(),'error');
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
        
        $model = new VideoModel();
        $model->author = session('savant.pid');
        $model->status = 2;
        try {
            $model->allowField(true)->save($data);
            
        } catch (\Exception $e) {
            Log::record('[视频管理]新增:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
        if($model->id){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $model = new VideoModel();
        try {
            $data = $request->param();
            if(strpos($data['thumb'],'base64')>0){
                $oss = new OssFlies();
                $url = 'zedu/uploads/video/'.date("Ymd").'/'.md5($data['thumb']).'.png';
                $thump = explode(',', $data['thumb']);
                $bool = $oss->stringOss($url, base64_decode($thump[1]));
                if($bool){
                    $data['thumb'] = $url;
                }else{
                    return ResultVOUtil::error(ResultEnum::OSS_UPLOAD_ERROR['code'],'上传封面失败');
                }
            }else{
                $data['thumb'] = 'zedu/uploads/aricle/4b439ff01dc4d6ec88af48a56ba04bb7.jpg';
            }
            $model->allowField(true)->save($data,['id'=>$id]);            
            return ResultVOUtil::success('');
        } catch (\Exception $e) {
            Log::record('[视频管理]修改:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
        }
        
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
